
###############################################################################
## CuteTimer
## Speed-run plugin for EventScripts Python
## Officially supported games: CS:S OB
##
## File: core/extender.py
## Author: zoomj
## Description: interface to aid in extending core
## Version: 0.1

import os
from __future__ import with_statement
from cutetimer.core.interface import interface
import utils.fileparser

class ExtensionManager(object):
    _extensions = {}
    
    def __init__(self):
        pass
        
    def __setattr__(self, name, value):
        if name == '_extensions':
            raise TypeError('extension list should not be removed')
        else:
            super(ExtensionManager, self).__setattr__(name, value)
        
    def __delattr__(self, name):
        if name == '_extensions':
            raise TypeError('extension list should not be removed')
        else:
            super(ExtensionManager, self).__delattr__(name)
            
    def load_extensions(self, extension):
        if len(self._extensions):
            pass

class ExtensionFileParser(fileparser.SimpleTextFileParser):
    def __init__(self, extensions_file):
        if not os.path.exists(extensions_file):
            raise IOError('extensions file could not be found')
        else:
            super(ExtensionFileParser, self).__init__(extensions_file, True)
            self._file_contents = [
                line for line in self._file_contents
                if self.valid_extension_name(line)
            ]

    @staticmethod
    def valid_extension_name(line):
        if not line.isalnum():
            if '_' in line:
                if line.startswith('_') or line.endswith('_'):
                    return False
                elif (not line.replace('_', '').isalnum() or '__' in line):
                    return False
        return True
